package cn.net.cfss.fgbp.face.manage.service;

import cn.net.cfss.fgbp.base.util.DateUtil;
import cn.net.cfss.fgbp.spi.manage.constant.OperationTypeEnum;
import cn.net.cfss.fgbp.spi.manage.dto.FscLogOperationDto;
import cn.net.cfss.fgbp.spi.manage.inte.IFscLogOperationService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLOutput;
import java.util.Arrays;
import java.util.Date;

/**
 * @author wutao
 * @version 1.0
 * @date 2020/10/13 14:24
 */
@Service
@Slf4j
public class OperationService {

    @Autowired
    private IFscLogOperationService fscLogOperationService;

    public void addOperationLog(OperationTypeEnum operationType, String opRemark, String oldData, String newData) {
        int dataMaxLength = 65535;
        if (oldData != null) {
            byte[] bytes = oldData.getBytes();
            if (bytes.length > dataMaxLength) {
                oldData = new String(Arrays.copyOf(bytes, dataMaxLength));
            }

        }
        if (newData != null) {
            byte[] bytes = newData.getBytes();
            if (bytes.length > dataMaxLength) {
                newData = new String(Arrays.copyOf(bytes, dataMaxLength));
            }
        }

        FscLogOperationDto fscLogOperationDto = new FscLogOperationDto();
        fscLogOperationDto.setOpType(operationType);
        fscLogOperationDto.setOpRemark(opRemark);
        fscLogOperationDto.setOldData(oldData);
        fscLogOperationDto.setNewData(newData);

        fscLogOperationService.add(fscLogOperationDto);

    }


    public void addOptLog(String remark,String tableName,Object obj)
    {
        JSONObject newData = new JSONObject();
        newData.put(tableName, obj);
        addOperationLog(OperationTypeEnum.ADD, remark, null, newData.toJSONString());
    }

    public void updateOptLog(String remark,String tableName,Object obj,Object oldObj)
    {
        JSONObject newData = new JSONObject();
        newData.put(tableName, obj);
        JSONObject oldData = new JSONObject();
        oldData.put(tableName, oldObj);
        addOperationLog(OperationTypeEnum.UPDATE, remark, oldData.toJSONString(), newData.toJSONString());
    }


    
}
